<?php
/**
* @version $Id: admin.header.php 10002 2008-02-08 10:56:57Z willebil $
* @package Joomla
* @subpackage header
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

// ensure user has access to this function
if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
		| $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_header' ))) {
	mosRedirect( 'index2.php', _NOT_AUTH );
}

require_once( $mainframe->getPath( 'admin_html' ) );
require_once( $mainframe->getPath( 'class' ) );

$cid = josGetArrayInts( 'cid' );

switch ($task) {

	case 'editA':
		editheader( $id, $option );
		break;

	case 'save':
		saveheader( $option, $id );
		break;
		

	case 'cancel':
		cancelheader();
		break;

	default:
		showheaders( $option );
		break;
}

/**
* List the records
* @param string The current GET/POST option
*/
function showheaders( $option ) {
	global $database, $mainframe, $mosConfig_list_limit;

	$id 		= intval( $mainframe->getUserStateFromRequest( "id{$option}", 'id', 0 ) );
	$limit 		= intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
	$limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );
	$search 	= $mainframe->getUserStateFromRequest( "search{$option}", 'search', '' );
	if (get_magic_quotes_gpc()) {
		$search	= stripslashes( $search );
	}

	if ( $search ) {
		$where[] = "cd.name LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
	}	
	if ( isset( $where ) ) {
		$where = "\n WHERE ". implode( ' AND ', $where );
	} else {
		$where = '';
	}

	// get the total number of records
	$query = "SELECT COUNT(*)"
	. "\n FROM #__header AS cd"
	. $where
	;
	$database->setQuery( $query );
	$total = $database->loadResult();

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

	// get the subset (based on limits) of required records
	$query = "SELECT *"
	. "\n FROM #__header AS cd"
	. $where
	. "\n ORDER BY cd.id, cd.name ASC"
	;
	$database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
	$rows = $database->loadObjectList();

	// build list of categories
	$javascript = 'onchange="document.adminForm.submit();"';
	$lists['catid'] = mosAdminMenus::ComponentCategory( 'catid', 'com_header_details', intval( $catid ), $javascript );

	HTML_header::showheaders( $rows, $pageNav, $search, $option, $lists );
}

/**
* Creates a new or edits and existing user record
* @param int The id of the record, 0 if a new entry
* @param string The current GET/POST option
*/
function editheader( $id, $option ) {
	global $database, $my;
	global $mosConfig_absolute_path;

	$row = new mosheader( $database );
	// load the row from the db table
	$row->load( (int)$id );

	if ($id) {
		// do stuff for existing records
		$row->checkout($my->id);
	} else {
		// do stuff for new records
		$row->imagepos = 'top';
		$row->headering = 0;
		$row->published = 1;
	}
	$lists = array();

	// build the html select list for headering
	$query = "SELECT headering AS value, name AS text"
	. "\n FROM #__header"
	. "\n WHERE published >= 0"
	. "\n AND id = " . (int) $row->id
	. "\n ORDER BY headering"
	;

	HTML_header::editheader( $row, $lists, $option, $params );
}

/**
* Saves the record from an edit form submit
* @param string The current GET/POST option
*/
function saveheader( $option ) {
	global $database, $mosConfig_absolute_path;

	josSpoofCheck();

	$row = new mosheader( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	// save params
	$params = mosGetParam( $_POST, 'params', '' );
	if (is_array( $params )) {
		$txt = array();
		foreach ( $params as $k=>$v) {
			$txt[] = "$k=$v";
		}
		$row->params = implode( "\n", $txt );
	}

	// pre-save checks
	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	$row->published = intval( mosGetParam( $_REQUEST, 'published', 0 ) );	
	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	mosRedirect( "index2.php?option=$option" );
}

function cancelheader() {
	global $database;

	josSpoofCheck();

	$row = new mosheader( $database );
	$row->bind( $_POST );
	$row->checkin();
	mosRedirect('index2.php?option=com_header');
}
?>
